import 'package:get/get.dart';
import 'package:number_spark_flutter/utils/ns_record_voice_utils.dart';

class NSRecordBottomSheetLogic extends GetxController {
  bool isRecording = false;
  int recordIngTime = 0;
  int maxRecordTime = 60;
  NSRecordVoiceUtils recordVoiceUtils = NSRecordVoiceUtils();
  NSRecordVoiceModel? recordVoiceModel;
  bool isMoveOut = false;

  @override
  void onClose() {
    super.onClose();
    recordVoiceUtils.stopRecord();
  }

  @override
  void onInit() {
    super.onInit();
    recordVoiceUtils.initRecord();
  }

  void startRecording() async {
    isRecording = true;
    recordIngTime = 0;
    update();
    recordVoiceUtils.start((time) {
      if (recordIngTime != time) {
        recordIngTime = time;
        if (recordIngTime >= maxRecordTime) {
          stopRecording();
        }
        update();
      }
    });
  }

  void stopRecording() async {
    isRecording = false;
    update();
    recordVoiceUtils.stopRecord(onSuccess: (NSRecordVoiceModel model) {
      if (model.status != -1) {
        recordVoiceModel = model;
        if (isMoveOut) {
          recordIngTime = 0;
          isMoveOut = false;
          update();
        }else{
          Get.back(result: recordVoiceModel);
        }
      }
    });
  }
}
